import { apiGetNewList } from '../../components/myrequest.js'

export default {
  namespaced: true,
  state: {
    // 频道列表数据源
    allDate: {}
  },
  mutations: {
    // 更新频道列表数据源
    updatelist (state, { currentCatagtory, list }) {
      state.allDate = {
        ...state.allDate,
        [currentCatagtory]: list
      }
    }
  },
  actions: {
    async gitNewList (context, caraId) {
      try {
        let res = await apiGetNewList(caraId)
        console.log(res)
        const results = res.data.data.results
        context.commit('updatelist', {
          // 传递给mutations
          currentCatagtory: caraId,
          list: results
        })
      } catch (err) {
        console.log('出错了', err)
      }

      // axios({
      //   url: `http://ttapi.research.itcast.cn/app/v1_1/articles?channel_id=${caraId}&timestamp=${Date.now()}&with_top=1`
      // })
    }

    // async gitNewList (context, cataId) {
    //   const {
    //     data: {
    //       data: { results }
    //     }
    //   } = await axios.get(
    //     `http://ttapi.research.itcast.cn/app/v1_1/articles?channel_id=${cataId}&timestamp=${Date.now()}&with_top=1`
    //   )
    //   // results是新闻列表
    //   context.commit('updatelist', { currentCatagtory: cataId, list: results })
    // }
  }
}
